﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;

namespace mlnx_os_sysdump_analyzer
{
    class Systemlog : IDisposable
    {
        private bool KEEP_RUNNING;
        private string OUTPUT_DIR_PATH;
        private DataTable fullContentTable = null;
        private DataTable imagesHistoryTable = null;
        //
        //----------------------------------------------------
        //
        public Systemlog(ref bool KEEP_RUNNING, ref string OUTPUT_DIR_PATH)
        {
            this.KEEP_RUNNING = KEEP_RUNNING;
            this.OUTPUT_DIR_PATH = OUTPUT_DIR_PATH;

            imagesHistoryTable = new DataTable();
            // add columns
            imagesHistoryTable.Columns.Add("Time", typeof(string));
            imagesHistoryTable.Columns.Add("Image", typeof(string));

            fullContentTable = new DataTable();
            // add columns
            fullContentTable.Columns.Add("Time", typeof(string));
            fullContentTable.Columns.Add("Message", typeof(string));
            // add DataRows.
        }
        //
        //----------------------------------------------------
        //
        public void scanInfo()
        {
            string path = Methods.FindFileRec(OUTPUT_DIR_PATH, "systemlog");
            if (path != null)
            {
                scanFile(path);
            }
        }
        //
        //----------------------------------------------------
        //
        private void scanFile(string srcFilePath)
        {
            if (srcFilePath == null)
                return;
            StreamReader sr = new StreamReader(srcFilePath);
            string line = "";
            string[] arr;
            try
            {
                while (KEEP_RUNNING && sr.Peek() >= 0)
                {
                    // read line
                    line = sr.ReadLine();
                    try
                    {
                        arr = Regex.Split(line, " ");
                        string time = "", image = "", full = "";
                        if (arr[0].Contains("/"))
                        { // the format is: YYYY/MM/DD hh:mm:ss
                            time = arr[0] + " " + arr[1];
                            for (int i = 2; i < arr.Length; i++)
                            {
                                // add to full
                                full += arr[i] + " ";

                                // add to image
                                if (i >= 7)
                                {
                                    image += arr[i] + " ";
                                }
                            }
                        }
                        else
                        { // the format is: DDD MMM DD hh:mm:ss
                            time = arr[0] + " " + arr[1] + " " + arr[2] + " " + arr[3];
                            for (int i = 4; i < arr.Length; i++)
                            {
                                // add to full
                                full += arr[i] + " ";
                            }
                        }


                        // add to full list
                        Item tmpFul = new Item(time, full);
                        fullContentTable.Rows.Add(tmpFul.itemName, tmpFul.content);

                        if (line.Contains("booting"))
                        {
                            // add to images list
                            Item tmpHis = new Item(time, image);
                            imagesHistoryTable.Rows.Add(tmpHis.itemName, tmpHis.content);

                        }
                    }//try
                    catch { }
                }// while
            }//try
            catch { }
            sr.Dispose();
        }
        //
        //----------------------------------------------------
        //
        public DataTable[] getSystemLog()
        {
            return new DataTable[] { imagesHistoryTable, fullContentTable };
        }
        //
        //----------------------------------------------------
        //

        public void Dispose()
        {
            if (imagesHistoryTable != null)
            {
                imagesHistoryTable.Dispose();
            }

            if (fullContentTable != null)
            {
                fullContentTable.Dispose();
            }
        }
        //----------------------------------------------------
    }
}
